// pages/saveCard/saveCard.js
import { list } from '../../../api/virtualStock'
import { summary } from '../../../api/emptyBarrel'
import util from '../../../utils/util'
import { list as productList } from '../../../api/product'
import {
  getInfo as mchGetInfo
} from '../../../api/enterprise'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productShow: false,
    emptyBarrelMany: 0,
    emptyBarrelLess: 0,
    price: 0.00,
    stock: 0,
    detailedList: [

    ],
    products: [],
    mch: {}
  },
  refundClick(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/member/order/refund?id=' + e.currentTarget.dataset.orderid + '&type=2'
    })
  },
  toBuy(e) {
    this.closeProduct()
    this.selectComponent('#bottomFrame').showFrame(e.currentTarget.dataset.id, "buyNow");
  },
  bucketClick() {
    this.selectComponent('#bucket').isShowClick()
  },


  loadEmptyBarrel() {
    summary().then(res => {
      this.data.emptyBarrelMany = res.data.emptyBarrelMany
      this.data.emptyBarrelLess = res.data.emptyBarrelLess
      this.data.price = res.data.price
      this.data.stock = res.data.stock
      this.setData({
        emptyBarrelMany: this.data.emptyBarrelMany,
        emptyBarrelLess: this.data.emptyBarrelLess,
        price: this.data.price.toFixed(2),
        stock: this.data.stock
      })
    })
    list({
      mch_id: app.globalData.mch_id,
      pageSize: 1000,
      subType: 3
    }).then(res => {
      this.data.detailedList = res.data.data
      this.setData({
        detailedList: this.data.detailedList
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadEmptyBarrel()
    this.getInfoClick()
    // this.loadProduct()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.loadEmptyBarrel()     
  },

  toBucket() {

  },
  toWithdrawal() {

  },
  toHistoty() {

  },

  //时间格式化器
  formatDate2day(val) {
    let date = new Date(val);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + MM + '-' + d;
  },

  formatDate2Time(val) {
    let date = new Date(val);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
  },

  getInfoClick() {
    let _this = this
    console.info("我被执行了")
    mchGetInfo({ "mchId": app.globalData.mchId }).then(
      res => {
        console.log(res)
        if (res.type == 'success') {

          _this.data.mch = res.data
          _this.setData({
            mch: _this.data.mch
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  phoneClick() {
    let _this = this
    console.log('2');
    console.log('111')
    if (_this.data.mch.phone) {
      wx.makePhoneCall({
        phoneNumber: _this.data.mch.phone // 仅为示例，并非真实的电话号码
      })
    }
  }

})